/**
 * Created by yunpeng on 2014/12/22.
 */
var debug = require('debug')('carClub');
var Table = require('../utils/commonDBConn').Table;

/**
 * 圈子（车友会）
 * @param name 车友会名称
 * @param ctitle 标题
 * @param content 内容
 * @param pic 图片路径
 * @param createtime 创建时间
 * @constructor
 */

var CarClub = function(name,title,contacts,tel,phone,address,content,pic,cid,createtime){
    this.id = null;
    this.name = name;
    this.title = title;
    this.contacts = contacts;//联系人
    this.tel = tel;
    this.phone = phone;
    this.address = address;
    this.content = content;
    this.pic = pic;
    this.cid = cid;
    this.createtime = createtime;

    if(typeof this.save!='function'){
        CarClub.prototype.save = function(callback){
            new Table('carclub').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getCarClubList !='function'){//
        CarClub.prototype.getCarClubList = function(params,page, callback){
            //SELECT cl.id,cl.title,cl.content,cl.pic,cl.cid,cl.createtime,c.province,c.city FROM carclub cl, citys c WHERE cl.cid=c.cid
            var columns = ' l.id,l.name,l.title,l.contacts,l.tel,l.phone,l.address,l.content,l.pic,l.cid,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,c.province,c.city ';
            var condition1 =' AND l.cid=c.cid ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.name LIKE '%"+params[2]+"%'";
            }
            new Table(' carclub l, citys c ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }

    if(typeof this.update !='function'){
        CarClub.prototype.update = function(callback){
            new Table('carclub').update('id', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.delCarClubs !='function'){
        CarClub.prototype.delCarClubs = function(ids, callback){
            new Table('carclub').delete('id', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }
};

/**
 * 自驾游
 * @param title 标题
 * @param content 内容
 * @param price 价格
 * @param starttime 开始时间
 * @param endtime 结束时间
 * @param count 报名人数
 * @param pic 图片地址
 * @param cid 所在城市
 * @param createtime 创建时间
 * @constructor
 */
var SelfDriving = function(title,content,price,starttime,endtime,count,pic,cid,createtime){
    this.id = null;
    this.title = title;
    this.content = content;
    this.price = price;
    this.starttime = starttime;
    this.endtime = endtime;
    this.count = count;
    this.pic = pic;
    this.cid = cid;
    this.createtime = createtime;

    if(typeof this.save!='function'){
        SelfDriving.prototype.save = function(callback){
            new Table('selfdriving').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getSdList !='function'){//
        SelfDriving.prototype.getSdList = function(params,page, callback){
            //SELECT cl.id,cl.title,cl.content,cl.pic,cl.cid,cl.createtime,c.province,c.city FROM carclub cl, citys c WHERE cl.cid=c.cid
            var columns = '  l.count,l.id,l.title,l.content,l.price,date_format(l.starttime,"%Y-%m-%d %H:%i:%s") as starttime, date_format(l.endtime,"%Y-%m-%d %H:%i:%s") as endtime,l.pic,l.cid,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,c.province,c.city ';
            var condition1 =' AND l.cid=c.cid ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.title LIKE '%"+params[2]+"%'";
            }
            if(params[3]){//移动端，客户所在城市
                condition1+=" and c.cid="+params[3];
            }
            new Table(' selfdriving l, citys c ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }

    if(typeof this.update !='function'){
        SelfDriving.prototype.update = function(callback){
            new Table('selfdriving').update('id', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.delSd !='function'){
        SelfDriving.prototype.delSd = function(ids, callback){
            new Table('selfdriving').delete('id', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getOrders != 'function'){
        SelfDriving.prototype.getOrders = function(option, callback){
            if(typeof callback != 'function'){
                callback = function(){};
            }
            var countSql = 'select count(*) count from user_seldrive where did=' + option.did;
            var sql = 'select * from user_seldrive, users where did=? and userid=users.id limit ?,?';
            var page = Number(option.page);
            var rows = Number(option.rows);
            page = (page-1)*rows;
            var id = Number(option.did);
            var args = [id, page, rows];
            new Table().getConnection(function(conn){
                if(conn){//没有数据库错误
                    var q1 = conn.query(countSql,function(err, data){
                        if(!err){
                            var count = data[0].count;
                            if(count != 0){
                                var q2 = conn.query(sql, args, function(err, data){
                                    if(!err){
                                        callback({total:count,rows:data})
                                    }else{
                                        debug(err);
                                        callback({err:'err',total:0,rows:[]});
                                    }
                                    releaseConnection(conn);
                                });
                                debug(q2.sql);
                            }else{
                                callback({total:0,rows:[]});
                                releaseConnection(conn);
                            }
                        }else{//数据库错误
                            debug(err);
                            releaseConnection(conn);
                            callback({err:'err',total:0,rows:[]});
                        }
                    });
                    debug(q1.sql);
                }else{
                    callback({err:'err',total:0,rows:[]});
                }
            })
        }
    }
};
function releaseConnection(conn){
    if(conn){
        conn.release();
    }
}
var Voice = function(title,content,total,currenttotal,starttime,endtime,pic,cid,createtime){
    this.id = null;
    this.title = title;
    this.content = content;
    this.total = total;
    this.currenttotal = currenttotal;
    this.starttime = starttime;
    this.endtime = endtime;
    //this.count = count;
    this.pic = pic;
    this.cid = cid;
    this.createtime = createtime;

    if(typeof this.save!='function'){
        Voice.prototype.save = function(callback){
            new Table('voice').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getVoiceList !='function'){//
        Voice.prototype.getVoiceList = function(params,page, callback){
            //SELECT cl.id,cl.title,cl.content,cl.pic,cl.cid,cl.createtime,c.province,c.city FROM carclub cl, citys c WHERE cl.cid=c.cid
            var columns = '  l.id,l.title,l.content,l.total,l.currenttotal,date_format(l.starttime,"%Y-%m-%d %H:%i:%s") as starttime, date_format(l.endtime,"%Y-%m-%d %H:%i:%s") as endtime,l.pic,l.cid,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,c.province,c.city ';
            var condition1 =' AND l.cid=c.cid ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.title LIKE '%"+params[2]+"%'";
            }
            if(params[3]){//移动端，客户所在城市
                condition1+=" and c.cid="+params[3];
            }
            new Table(' voice l, citys c ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }

    if(typeof this.update !='function'){
        Voice.prototype.update = function(callback){
            new Table('voice').update('id', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.delVoice !='function'){
        Voice.prototype.delVoice = function(ids, callback){
            new Table('voice').delete('id', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }
};


var Activity = function(title,content,total,currenttotal,starttime,endtime,pic,cid,createtime){
    this.id = null;
    this.title = title;
    this.content = content;
    this.total = total;
    this.currenttotal = currenttotal;
    this.starttime = starttime;
    this.endtime = endtime;
    //this.count = count;
    this.pic = pic;
    this.cid = cid;
    this.createtime = createtime;

    if(typeof this.save!='function'){
        Activity.prototype.save = function(callback){
            new Table('activity').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getActivityList !='function'){//
        Activity.prototype.getActivityList = function(params,page, callback){
            //SELECT cl.id,cl.title,cl.content,cl.pic,cl.cid,cl.createtime,c.province,c.city FROM carclub cl, citys c WHERE cl.cid=c.cid
            var columns = '  l.id,l.title,l.content,l.total,l.currenttotal,date_format(l.starttime,"%Y-%m-%d %H:%i:%s") as starttime, date_format(l.endtime,"%Y-%m-%d %H:%i:%s") as endtime,l.pic,l.cid,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,c.province,c.city ';
            var condition1 =' AND l.cid=c.cid ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.title LIKE '%"+params[2]+"%'";
            }
            new Table(' activity l, citys c ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }

    if(typeof  this.getOrders != 'function'){
        Activity.prototype.getOrders = function(option, callback){
            if(typeof callback != 'function'){
                callback = function(){};
            }
            var countSql = 'select count(*) count from user_activity where aid=' + option.aid;
            var sql = 'select * from user_activity, users where aid=? and userid=users.id limit ?,?';
            var page = Number(option.page);
            var rows = Number(option.rows);
            page = (page-1)*rows;
            var id = Number(option.aid);
            var args = [id, page, rows];
            new Table().getConnection(function(conn){
                if(conn){//没有数据库错误
                    var q1 = conn.query(countSql,function(err, data){
                        if(!err){
                            var count = data[0].count;
                            if(count != 0){
                                var q2 = conn.query(sql, args, function(err, data){
                                    if(!err){
                                        callback({total:count,rows:data})
                                    }else{
                                        debug(err);
                                        callback({err:'err',total:0,rows:[]});
                                    }
                                    releaseConnection(conn);
                                });
                                debug(q2.sql);
                            }else{
                                callback({total:0,rows:[]});
                                releaseConnection(conn);
                            }
                        }else{//数据库错误
                            debug(err);
                            releaseConnection(conn);
                            callback({err:'err',total:0,rows:[]});
                        }
                    });
                    debug(q1.sql);
                }else{
                    callback({err:'err',total:0,rows:[]});
                }
            })
        }
    }

    if(typeof this.update !='function'){
        Activity.prototype.update = function(callback){
            new Table('activity').update('id', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.delVoice !='function'){
        Activity.prototype.delVoice = function(ids, callback){
            new Table('activity').delete('id', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }
};


module.exports.CarClub  = CarClub;
module.exports.SelfDriving = SelfDriving;

module.exports.Voice = Voice;
module.exports.Activity = Activity;